package com.android.chat.example;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.packet.Presence;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.android.chat.connection.ServerConnect;

public class ListFriend extends ListActivity {

	ListView lv;
	List<Friend> lstFriends;
	Connection conn;
	ListAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		lstFriends = new ArrayList<Friend>();

		conn = ServerConnect.getConnection();
		Roster roster = conn.getRoster();

		Collection<RosterEntry> entries = roster.getEntries();
		for (RosterEntry entry : entries) {
			Friend friend = new Friend();

			friend.setName(entry.getName());
			friend.setUsername(entry.getUser());
			if (entry.getStatus() != null)
				friend.setStatus(entry.getStatus().toString());

			lstFriends.add(friend);
		}

		adapter = new ArrayAdapter<Friend>(this, R.layout.friend_row,
				lstFriends) {
			public View getView(int position, View convertView,
					android.view.ViewGroup parent) {

				View v = convertView;
				if (v == null) {
					LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					v = vi.inflate(R.layout.friend_row, null);
				}
				Friend f = lstFriends.get(position);
				if (f != null) {
					TextView txtName = (TextView) v.findViewById(R.id.name);
					TextView txtUsername = (TextView) v
							.findViewById(R.id.username);
					TextView txtStatus = (TextView) v.findViewById(R.id.status);

					if (txtName != null) {
						txtName.setText("Name: " + f.getName());
					}
					if (txtUsername != null) {
						txtUsername.setText("Username: " + f.getUsername());
					}

					if (txtStatus != null) {
						Presence p = conn.getRoster().getPresence(
								f.getUsername());

						txtStatus.setText("Online: " + p.isAvailable());
					}
				}
				return v;

			}
		};

		roster.addRosterListener(new RosterListener() {

			@Override
			public void presenceChanged(Presence presence) {

			}

			@Override
			public void entriesUpdated(Collection<String> arg0) {

			}

			@Override
			public void entriesDeleted(Collection<String> arg0) {

			}

			@Override
			public void entriesAdded(Collection<String> arg0) {

			}
		});

		setListAdapter(adapter);

		lv = getListView();
		lv.setTextFilterEnabled(true);

		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(getApplicationContext(),
						ChatActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString(ChatActivity.CHAT_ID_KEY,
						lstFriends.get(position).getUsername());
				bundle.putString("CHAT_BUDDY", lstFriends.get(position).getName());
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});
	}

}
